﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using StockStar.GatherData;
using System.Data;

namespace Report
{
    public class StockBan
    {
        public string StrConnection = SQLHelper.StrConn;
        public string Reg = "<tr class=\"((list_eve)|(list_odd))\">\\s+(<td[^>]*>(.*)<\\/td>\\s+){10}</tr>";
        public string Url = "http://data.eastmoney.com/dxf/default.html";
        public void Import()
        {
            WebClient client = new WebClient();
            string str = client.DownloadString(Url);

            var mc = Regex.Matches(str, Reg);
            for (int i = 0; i < mc.Count; i++)
            {
                StockBanEntity c = new StockBanEntity();
                XmlDocument doc = new XmlDocument();
                string aa = mc[i].Value.Replace("&", "&amp;");
                doc.LoadXml(aa);
                var root = doc.DocumentElement;
                c.StockId = root.ChildNodes[1].ChildNodes[0].InnerText;
                c.StockName = root.ChildNodes[2].ChildNodes[0].InnerText;
                string time = root.ChildNodes[4].InnerText;
                try
                {
                    c.JCXSRQ = DateTime.Parse(time);
                }
                catch
                {
                }
                try
                {
                    string s = root.ChildNodes[6].InnerText;
                    c.SL = decimal.Parse(s);
                }
                catch
                {
                }
                try
                {
                    string s = root.ChildNodes[5].InnerText;
                    c.ZZGBBL = decimal.Parse(s);
                }
                catch
                {
                }
                Insert(c);
            }
        }

        private void Insert(StockBanEntity c)
        {
            string sql2 = "select * from dbo.StockBan where StockId=@StockId and JCXSRQ=@JCXSRQ";
            Tools tool2 = new Tools();
            tool2.AddSqlParameter("StockId", System.Data.SqlDbType.VarChar, c.StockId);
            tool2.AddSqlParameter("JCXSRQ", System.Data.SqlDbType.VarChar, c.JCXSRQ);
            DataSet ds = SQLHelper.ExecuteSet(StrConnection, System.Data.CommandType.Text, sql2, tool2.GetSqlParameters());
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return;
                }
            }
            string sql = "INSERT INTO [StockBan]([StockId],[StockName],[JCXSRQ],[ZZGBBL],[SL])VALUES(@StockId,@StockName,@JCXSRQ,@ZZGBBL,@SL)";
            Tools tool = new Tools();
            tool.AddSqlParameter("StockId", System.Data.SqlDbType.VarChar, c.StockId);
            tool.AddSqlParameter("StockName", System.Data.SqlDbType.VarChar, c.StockName);
            tool.AddSqlParameter("JCXSRQ", System.Data.SqlDbType.DateTime, c.JCXSRQ);
            tool.AddSqlParameter("ZZGBBL", System.Data.SqlDbType.Decimal, c.ZZGBBL);
            tool.AddSqlParameter("SL", System.Data.SqlDbType.Decimal, c.SL);
            SQLHelper.ExecuteNonQuery(StrConnection, System.Data.CommandType.Text, sql, tool.GetSqlParameters());
        }


    }

    public class StockBanEntity
    {
        /// <summary>
        /// 股票代码
        /// </summary>
        public string StockId
        {
            get;
            set;
        }

        /// <summary>
        /// 股票名称
        /// </summary>
        public string StockName
        {
            get;
            set;
        }

        /// <summary>
        /// 解除限售日期
        /// </summary>
        public DateTime? JCXSRQ
        {
            get;
            set;
        }

        /// <summary>
        /// 占总股本比例
        /// </summary>
        public decimal ZZGBBL
        {
            get;
            set;
        }

        /// <summary>
        /// 数量（万股）
        /// </summary>
        public decimal SL
        {
            get;
            set;
        }
    }
}
